Passed
Push — feature/job-summary-hr ( 67ecfb )
by Grant
13:42
created

queries.ts ➔ uniq   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
1
/**
2
 * Shortcut function that returns the id attribute of an object.
3
 * @param item
4
 */
5
export function getId<T extends { id: number }>(item: T): number {
6
  return item.id;
7
}
8
9
export function identity<T>(value: T): T {
10
  return value;
11
}
12
13
/**
14
 * Returns true as long as the value passed to it is not null or undefined.
15
 * Can be used to filter nulls and undefined values out of an array.
16
 * @param item
17
 */
18
export function notEmpty<T>(value: T | null | undefined): value is T {
19
  return value !== null && value !== undefined;
20
}
21
22
/**
23
 * From an array of objects, return the first object with a specific id value.
24
 * @param objs
25
 * @param id
26
 */
27
export function find<T extends { id: number }>(
28
  objs: T[],
29
  id: number,
30
): T | null {
31
  const found = objs.filter(item => item.id === id);
32
  return found.length > 0 ? found[0] : null;
33
}
34
35
/**
36
 * Return all objects from array with the specified key-value attribute.
37
 * @param objs
38
 * @param prop
39
 * @param value
40
 */
41
export function where<T, K extends keyof T>(
42
  objs: T[],
43
  prop: K,
44
  value: any,
0 ignored issues
show
introduced by
Unexpected any. Specify a different type.
Loading history...
45
): T[] {
46
  return objs.filter(obj => prop in obj && obj[prop] === value);
47
}
48
49
/**
50
 * Return first object from array with the specified key-value attribute.
51
 * @param objs
52
 * @param prop
53
 * @param value
54
 */
55
export function whereFirst<T, K extends keyof T>(
56
  objs: T[],
57
  prop: K,
58
  value: any,
0 ignored issues
show
introduced by
Unexpected any. Specify a different type.
Loading history...
59
): T {
60
  return where(objs, prop, value)[0];
61
}
62
63
/**
64
 * Map each key-value attribute of an object into an array
65
 * @param object
66
 * @param mapFn
67
 */
68
export function objectMap<A, B>(
69
  object: {
70
    [key: string]: A;
71
  },
72
  mapFn: (key: string, value: A) => B,
73
): B[] {
74
  return Object.keys(object).reduce((result: B[], key: string): B[] => {
75
    result.push(mapFn(key, object[key]));
76
    return result;
77
  }, []);
78
}
79
80
interface IndexedObject<T> {
81
  [key: string]: T;
82
}
83
84
/**
85
 * Maps an array of items into an object, with each transformed into an attribute
86
 * @param items array of objects
87
 * @param keyFn Function that returns a unique key for each object. Typically the getId function.
88
 * @param valFn Function that returns a new value for each object.
89
 */
90
export function mapToObjectTrans<A, B>(
91
  items: A[],
92
  keyFn: (item: A) => string | number,
93
  valFn: (item: A) => B,
94
): IndexedObject<B> {
95
  return items.reduce((result: IndexedObject<B>, item: A): IndexedObject<B> => {
96
    const key = keyFn(item);
97
    result[key] = valFn(item);
0 ignored issues
show
introduced by
Assignment to property of function parameter 'result'.
Loading history...
98
    return result;
99
  }, {});
100
}
101
102
/**
103
 * Maps an array of items into an object, with each item set as an attribute.
104
 * @param items array of objects
105
 * @param keyFn Function that returns a unique key for each object. Typically the getId function.
106
 */
107
export function mapToObject<T>(
108
  items: T[],
109
  keyFn: (item: T) => string | number,
110
): IndexedObject<T> {
111
  return mapToObjectTrans(items, keyFn, (item): T => item);
112
}
113
114
/**
115
 * Checks if an object has an attribute with a particular key
116
 * @param object
117
 * @param key
118
 */
119
export function hasKey<T>(
120
  object: { [key: string]: T },
121
  key: string | number,
122
): boolean {
123
  return Object.prototype.hasOwnProperty.call(object, key);
124
}
125
126
/**
127
 * Returns the value at the specified key. If the key is not present, throws an error.
128
 * @param object
129
 * @param key
130
 * @param errorMessage
131
 */
132
export function getOrThrowError<T>(
133
  object: { [key: string]: T },
134
  key: string | number,
135
  errorMessage: string,
136
): T {
137
  if (!hasKey(object, key)) {
138
    throw new Error(errorMessage);
139
  }
140
  return object[key];
141
}
142
143
/** Return a copy of the object with specific property removed */
144
export function deleteProperty<T>(
145
  obj: IndexedObject<T>,
146
  key: string | number,
147
): IndexedObject<T> {
148
  const { [key]: _, ...newObj } = obj;
0 ignored issues
show
introduced by
'_' is assigned a value but never used.
Loading history...
149
  return newObj;
150
}
151
152
/**
153
 * Iterate through the properties of the object, checking which return true for the filterFunction.
154
 * Return a copy of the object, without the properties that don't pass the filter.
155
 */
156
export function filterObjectProps<T>(
157
  obj: IndexedObject<T>,
158
  filter: (value: T) => boolean,
159
): IndexedObject<T> {
160
  return Object.entries(obj).reduce(
161
    (newObj: IndexedObject<T>, [key, value]): IndexedObject<T> => {
162
      if (filter(value)) {
163
        newObj[key] = value;
0 ignored issues
show
introduced by
Assignment to property of function parameter 'newObj'.
Loading history...
164
      }
165
      return newObj;
166
    },
167
    {},
168
  );
169
}
170
171
/** Return a copy of the list with duplicate elements removed */
172
export function uniq<T>(x: T[]): T[] {
173
  return Array.from(new Set(x));
174
}
175